﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IRepository.LY;
using DFGK.IService.LY;
using DFGK_WMS.Model.Models.LyModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK.Service.LY
{
    public class InventoryInitializationService : BaseService, IInventoryInitializationService
    {
        private readonly IInventoryInitializationRepository inventoryInitialization;
        private readonly IInventoryRepository inventoryRepository;
        private readonly IUnitOfWork unitOfWork;
        public InventoryInitializationService(IMapper mapper, IInventoryInitializationRepository inventoryInitialization, IUnitOfWork unitOfWork, IInventoryRepository inventoryRepository) : base(mapper)
        {
            this.inventoryInitialization = inventoryInitialization;
            this.inventoryRepository = inventoryRepository;
            this.unitOfWork = unitOfWork;
        }

        public bool AddOrModefiyInventoryInitialization(InventoryInitialization model, out string errorMsg)
        {
            errorMsg = string.Empty;
            if (model.Id > 0)
            {
                inventoryInitialization.UpdEntity(model);
            }
            else
            {
                inventoryInitialization.AddEntity(model);

            }
            return unitOfWork.SaveChangesEntity();
        }

        public bool DelInventoryInitialization(long inventoryInitializationId)
        {
            var Quote = inventoryInitialization.QueryEntityById(inventoryInitializationId);
            if (Quote == null)
            {
                return false;
            }
            inventoryInitialization.DelEntity(Quote);
            return unitOfWork.SaveChangesEntity();
        }

        public InventoryInitialization? GetInventoryInitializationById(long inventoryInitializationId)
        {
            return inventoryInitialization.QueryEntityById(inventoryInitializationId);
        }

        public List<InventoryInitialization> GetInventoryInitializationData(string? name, out int totalCount, int pageIndex = 1, int pageSize = 15)
        {
            var list = inventoryInitialization.GetEntityAlls();
            if (!string.IsNullOrEmpty(name))
            {
                list = list.Where(d => d.WarehouseName.Contains(name));
            }
            totalCount = list.Count();
            list = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).OrderByDescending(d => d.Id);
            return list.ToList();
        }

        public bool InitializeInventory(long id, out string msg)
        {
            msg = string.Empty;
            var obj = inventoryInitialization.QueryEntityById(id);
            if(obj == null)
            {
                msg = "找不到单据！";
                return false;
            }
            var invobj = inventoryRepository.QueryEntityWhere(x => x.MaterialName == obj.MaterialName);
            if(invobj == null)
            {
                msg = "库存中没有可初始化的信息！";
                return false;
            }
            invobj.AvailableQty = obj.Quantity;
            invobj.Price = obj.CostPrice;
            inventoryRepository.UpdEntity(invobj);
            return unitOfWork.SaveChangesEntity();
        }
    }
}
